# SPDX-FileCopyrightText: Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 2.8.12)
project(demo CUDA CXX C)
enable_language("CUDA")

set(Protobuf_DIR "/usr/src/protobuf_3.6.1/usr/local")
set(Protobuf_PREFIX_PATH
"/usr/src/protobuf_3.6.1/usr/local/include"
"/usr/src/protobuf_3.6.1/usr/local/lib"
"/usr/src/protobuf_3.6.1/usr/local/bin"
)
link_directories(/usr/src/protobuf_3.6.1/usr/local/lib)
list(APPEND CMAKE_PREFIX_PATH "${Protobuf_PREFIX_PATH}")
set(Protobuf_PROTOC_EXECUTABLE "/usr/src/protobuf_3.6.1/usr/local/bin/protoc")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "/opt/conda/lib/python3.8/site-packages/torch/share/cmake/Torch")
set(OpenCV_DIRS /usr/local/include/opencv4)

# find_package(yaml-cpp REQUIRED)
find_package(Protobuf REQUIRED)
find_package(CUDA REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Eigen3 REQUIRED)
# find_package(Torch REQUIRED)

message(STATUS "yaml-cpp dirs: ${Torch_LIBRARY_DIR}")
include_directories(${OpenCV_INCLUDE_DIRS} ${Protobuf_INCLUDE_DIRS} ${TORCH_INCLUDE_DIRS} ${EIGEN3_INCLUDE_DIRS} ${YAML_CPP_INCLUDE_DIRS}
                    /usr/src/dependencies/stb
                    /cv/yaml-cpp-yaml-cpp-0.6.2/include
                    /opt/conda/lib/python3.8/site-packages/torch/include

                )



if(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL  "aarch64")
  message("Using aarch64 Mode")
  set(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc)
  set(CMAKE_CXX_COMPILER /usr/bin/aarch64-linux-gnu-g++)
  set(CUDA_INSTALL_TARGET_DIR targets/aarch64-linux)
elseif(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  message("Using x86_64 Mode")
  set(CMAKE_C_COMPILER /usr/bin/gcc)
  set(CMAKE_CXX_COMPILER /usr/bin/g++)
  set(CUDA_INSTALL_TARGET_DIR targets/x86_64-linux)
endif()

set(CUDA_TOOLKIT_ROOT_DIR /usr/local/cuda)
set(CUDA_INCLUDE_DIRS ${CUDA_TOOLKIT_ROOT_DIR}/${CUDA_INSTALL_TARGET_DIR}/include)
# set PyTorch path
# set(Torch_DIR "/home/nvidia/.local/lib/python3.8/site-packages/torch/share/cmake/Torch")
# find_package(Torch REQUIRED PATH "/home/nvidia/.local/lib/python3.8/site-packages/torch/share/cmake/Torch")

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_RELEASE "-Wno-deprecated-declarations -O2")
# add_compile_options(-W)
# add_compile_options(-std=c++14)

set( SMS 30 32 35 37 50 52 53 60 61 62 70 72 75 87)
foreach(sm ${SMS})
	set(GENCODE ${GENCODE} -gencode arch=compute_${sm},code=sm_${sm})
endforeach()
set(HIGHEST_SM 87)
set(GENCODE ${GENCODE} -gencode arch=compute_${HIGHEST_SM},code=compute_${HIGHEST_SM})

set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS}
    -ccbin ${CMAKE_CXX_COMPILER}
    -Xcompiler -DWIN_INTERFACE_CUSTOM
    -Xcompiler -I/usr/aarch64-linux-gnu/include/
    -Xlinker -lsocket
    -Xlinker -rpath=/usr/lib/aarch64-linux-gnu/
    -Xlinker -rpath=/usr/aarch64-linux-gnu/lib/
    -Xlinker -L/usr/lib/aarch64-linux-gnu/
    -Xlinker -L/usr/aarch64-linux-gnu/lib/
)

if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
  message("Using Debug Mode")
  set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -g -G --ptxas-options=-v)
endif()

set(CMAKE_CUDA_COMPILER  /usr/local/cuda/bin/nvcc)
set(CMAKE_CUDA_STANDARD 14)
# set(TENSORRT_INCLUDE_DIRS /usr/include/aarch64-linux-gnu/)
# set(TENSORRT_LIBRARY_DIRS /usr/lib/aarch64-linux-gnu/)

set(TENSORRT_INCLUDE_DIRS /usr/src/TensorRT/include)
set(TENSORRT_LIBRARY_DIRS /usr/src/TensorRT/lib)
include_directories(
    /usr/local/include
    ${CUDA_INCLUDE_DIRS}
    ${TENSORRT_INCLUDE_DIRS}
    include
    src/common
    src
    dependencies/stb
    libraries/cuOSD/src
)

link_directories(
	${TENSORRT_LIBRARY_DIRS}
	/usr/lib/aarch64-linux-gnu
    /usr/aarch64-linux-gnu/lib/
    /home/nvidia/.local/lib/python3.8/site-packages/torch/lib
    build/
    /usr/local/cuda/include/
    /usr/src/TensorRT/include/
    /usr/src/dependencies/stb
    /usr/local/cuda/lib64
    /usr/local/lib
)

file(GLOB_RECURSE SOURCE_FILES
    src/*.cu
    src/*.cpp
    libraries/cuOSD/src/*.cpp
    libraries/cuOSD/src/*.cu
)


####################### libCostVolume.so ##########################
file(GLOB_RECURSE Cost_Plugin_SOURCE_FILES 
    src/build_cost_volume/*.cu
    src/build_cost_volume/*.cpp
    src/common/tensorrt.cpp
)
add_library(CostVolumePlugin MODULE ${Cost_Plugin_SOURCE_FILES})
target_compile_features(CostVolumePlugin PUBLIC cxx_std_14)
target_link_libraries(CostVolumePlugin nvinfer cudart nvinfer_plugin) 
# ####################### libDpsCostVolume.so ##########################
file(GLOB_RECURSE Dps_Cost_Plugin_SOURCE_FILES 
    src/build_dps_cost_volume/*.cu
    src/build_dps_cost_volume/*.cpp
    src/common/tensorrt.cpp
)
add_library(DpsCostVolumePlugin MODULE ${Dps_Cost_Plugin_SOURCE_FILES})
target_compile_features(DpsCostVolumePlugin PUBLIC cxx_std_14)
target_link_libraries(DpsCostVolumePlugin nvinfer cudart nvinfer_plugin) 

# ####################### libDpsGeoVolume.so ##########################
file(GLOB_RECURSE Dps_Geo_Plugin_SOURCE_FILES 
    src/build_dps_geometry_volume/*.cu
    src/build_dps_geometry_volume/*.cpp
    src/common/tensorrt.cpp
)
add_library(DpsGeometryVolumePlugin MODULE ${Dps_Geo_Plugin_SOURCE_FILES})
target_compile_features(DpsGeometryVolumePlugin PUBLIC cxx_std_14)
target_link_libraries(DpsGeometryVolumePlugin nvinfer cudart) 

####################### libGridSampler.so ##########################
file(GLOB_RECURSE Grid_Sampler_Plugin_SOURCE_FILES 
    src/GridSamplePlugin/*.cu
    src/GridSamplePlugin/*.cpp
    src/common/tensorrt.cpp
)
add_library(GridSamplePlugin MODULE ${Grid_Sampler_Plugin_SOURCE_FILES})
target_compile_features(GridSamplePlugin PUBLIC cxx_std_14)
target_link_libraries(GridSamplePlugin nvinfer cudart nvinfer_plugin) 

####################### libDeformConv.so ##########################
file(GLOB_RECURSE Deform_Conv_Plugin_SOURCE_FILES 
    src/deform_conv/*.cu
    src/deform_conv/*.cpp
    src/common/tensorrt.cpp
    src/trt_cuda_helper.cu)
add_library(DeformConvPlugin MODULE ${Deform_Conv_Plugin_SOURCE_FILES})
target_compile_features(DeformConvPlugin PUBLIC cxx_std_14)
target_link_libraries(DeformConvPlugin nvinfer cudart cublas nvinfer_plugin) 


# add_executable(export
#     build_engine2.cpp
#     ${Cost_Plugin_SOURCE_FILES}
#     # ${Dps_Cost_Plugin_SOURCE_FILES}
#     # ${Dps_Geo_Plugin_SOURCE_FILES}
#     ${Grid_Sampler_Plugin_SOURCE_FILES}
#     ${Deform_Conv_Plugin_SOURCE_FILES}
#     )

# target_link_libraries(export
#     libnvinfer_plugin.so.8
#     yaml-cpp 
#     libnvinfer.so
#     libnvonnxparser.so
#     libcublas.so
#     )

# add_executable(infer
#     stereo.cpp
#     # /data/test_yc/tensorrt_dsgn2/src/postprocess/postprocess.cpp
#     ${SOURCE_FILES}
#     )

# target_link_libraries(infer
#     libnvinfer_plugin.so.8
#     libCostVolumePlugin.so
#     # libDpsCostVolume.so
#     # libDpsGeoVolume.so
#     libGridSamplePlugin.so
#     libDeformConvPlugin.so
#     yaml-cpp 
#     libnvinfer.so
#     libnvonnxparser.so
#     # c10.so
#     libcublas.so
#     Eigen3::Eigen
#     ${OpenCV_LIBS}
#     torch
#     )

# add_executable(test_preprocess
#     test_preprocess.cpp
#     ${SOURCE_FILES}
#     )

# target_link_libraries(infer
#     libnvinfer_plugin.so.8
#     libCostVolumePlugin.so
#     # libDpsCostVolume.so
#     # libDpsGeoVolume.so
#     libGridSamplePlugin.so
#     libDeformConvPlugin.so
#     yaml-cpp 
#     libnvinfer.so
#     libnvonnxparser.so
#     # c10.so
#     libcublas.so
#     Eigen3::Eigen
#     ${OpenCV_LIBS}
#     torch
#     )
